Completed
Push — master ( 87ce00...e4d8c7 )
by Justin
01:31
created

Identifiers.js ➔ Identifiers   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
c 1
b 0
f 1
nc 6
nop 1
dl 0
loc 29
rs 6.7272
1
var utils = require('./utils'),
2
    Base = require('./Base');
3
4
/**
5
 * Manage the set of identifers for an object.
6
 * 
7
 * @constructor
8
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
9
 */
10
var Identifiers = function(json){
11
  
12
  // Protect against forgetting the new keyword when calling the constructor
13
  if(!(this instanceof Identifiers)){
14
    return new Identifiers(json);
15
  }
16
  
17
  // If the given object is already an instance then just return it. DON'T copy it.
18
  if(Identifiers.isInstance(json)){
19
    return json;
20
  }
21
  
22
  Base.call(this, json);
23
  
24
  this.identifiers = {};
25
  
26
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
27
    this.identifiers = json;
28
    
29
    // The spec allows for types with single values to "forgo the array and use
30
    // a single string" but that's a pain to keep track of so we're going to
31
    // convert all single strings into arrays
32
    for(var a in this.identifiers){
33
      if(this.identifiers.hasOwnProperty(a) && !Array.isArray(this.identifiers[a])){
34
        this.identifiers[a] = [ this.identifiers[a] ];
35
      }
36
    }
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
37
  }
38
};
39
40
Base.prototype = Object.create(Base.prototype);
41
42
Identifiers._gedxClass = Identifiers.prototype._gedxClass = 'GedcomX.Identifiers';
43
44
/**
45
 * Check whether the given object is an instance of this class.
46
 * 
47
 * @param {Object} obj
48
 * @returns {Boolean}
49
 */
50
Identifiers.isInstance = function(obj){
51
  return utils.isInstance(obj, this._gedxClass);
52
};
53
54
/**
55
 * Get the values for a given type
56
 * 
57
 * @param {String=} type If not specified then values with no associated type
58
 * are returned.
59
 * @return {String[]}
60
 */
61
Identifiers.prototype.getValues = function(type){
62
  if(typeof type === 'undefined'){
63
    type = '$';
64
  }
65
  return this.identifiers[type] || [];
66
};
67
68
/**
69
 * Set the values for a given type
70
 * 
71
 * @param {String[]} values
72
 * @param {String=} type
73
 */
74
Identifiers.prototype.setValues = function(values, type){
75
  if(typeof type === 'undefined'){
76
    type = '$';
77
  }
78
  if(Array.isArray(values)){
79
    this.identifiers[type] = values;
80
  }
81
  return this;
82
};
83
84
/**
85
 * Add a value for a given type
86
 * 
87
 * @param {String} value
88
 * @param {String=} type
89
 */
90
Identifiers.prototype.addValue = function(value, type){
91
  if(typeof type === 'undefined'){
92
    type = '$';
93
  }
94
  if(!Array.isArray(this.identifiers[type])){
95
    this.identifiers[type] = [];
96
  }
97
  this.identifiers[type].push(value);
98
  return this;
99
};
100
101
/**
102
 * Export the object as JSON
103
 * 
104
 * @return {Object} JSON object
105
 */
106
Identifiers.prototype.toJSON = function(){
107
  return this.identifiers;
108
};
109
110
module.exports = Identifiers;